package com.mingtu.jgspatrol.activity;

import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mingtu.common.Eventbus.MyEventBus;
import com.mingtu.common.adapter.MyFragmentPagerAdapter;
import com.mingtu.common.entity.TabEntity;
import com.mingtu.common.utils.ActivityUtil;
import com.mingtu.common.utils.IntentUtils;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.base.MyBaseActivity;
import com.mingtu.jgspatrol.databinding.ActivityReportedEventLeaderBinding;
import com.mingtu.jgspatrol.fragment.FinishReportEventFragment;
import com.mingtu.jgspatrol.fragment.UndoReportEventFragment;
import com.mingtu.jgspatrol.utils.MyConstant;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

public class ReportedEventLeaderActivity extends MyBaseActivity<ActivityReportedEventLeaderBinding> {
    private static final String[] mTitles = {"待处理", "已处理"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private FinishReportEventFragment finishReportEventFragment;
    private UndoReportEventFragment undoReportEventFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_reported_event_leader;
    }

    @Override
    protected void initView() {
        super.initView();
        setModuleTitle("上报事件");
    }

    @Override
    protected void initData() {
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }
        undoReportEventFragment = UndoReportEventFragment.getInstance();
        finishReportEventFragment = FinishReportEventFragment.getInstance();
        mFragments.add(undoReportEventFragment);
        mFragments.add(finishReportEventFragment);

        binding.viewPager.setOffscreenPageLimit(2);
        binding.viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments));
        binding.viewPager.setCurrentItem(0);
        binding.viewPager.addOnPageChangeListener(onPageChangeListener);

        binding.commonTab.setTextBold(1);
        binding.commonTab.setTabData(mTabEntities);
        binding.commonTab.setOnTabSelectListener(onTabSelectListener);
        EventBus.getDefault().register(this);
        addClickListener(this, binding.ivPlus);
    }

    @Override
    protected void getData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe
    public void onStringEvent(MyEventBus event) {
        //处理完之后刷新数据
        String message = event.getMessgae();
        if (message.equals(MyConstant.REFRESH_REPORTED_DATA)) {
            try {
                undoReportEventFragment.refresh();
                finishReportEventFragment.refresh();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private OnTabSelectListener onTabSelectListener = new OnTabSelectListener() {
        @Override
        public void onTabSelect(int position) {
            binding.viewPager.setCurrentItem(position, false);
        }

        @Override
        public void onTabReselect(int position) {

        }
    };

    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            binding.commonTab.setCurrentTab(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_plus:
                IntentUtils.getInstance().readyGo(ActivityUtil.getTopActivity(), ReportedEventActivity.class);
                break;
        }
    }

}